package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     * @param res
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest res, @RequestBody Employee employee){
        log.info("登录");
        R<Employee> r = employeeService.login(res,employee);
        return r;
    }

    /**
     * 退出
     * @param res
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest res){
        log.info("退出");
        R<String> r = employeeService.logout(res);
        return r;
    }

    /**
     * 新增员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);
        log.info("新增员工，员工信息：{}",employee.toString());
        R<String> r = employeeService.save(request, employee);
        return r;
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page:{},pageSize:{},name:{}", page, pageSize, name);
        R<Page> r = employeeService.page(page, pageSize, name);
        return r;
    }

    /**
     * 根据id修改员工信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);
        log.info(employee.toString());
        R<String> r = employeeService.update(request, employee);
        return r;
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("根据id查询员工信息...");
        R<Employee> r = employeeService.selectById(id);
        return r;
    }
}
